package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.common.ParamPage;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public List<Setmeal> findByCid(Long id,Integer status) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(status!=null,Setmeal::getStatus,status);
        lqw.eq(Setmeal::getCategoryId,id);
        return setmealDao.selectList(lqw);
    }

    @Override
    public IPage<SetmealDto> findPage(ParamPage paramPage) {
        IPage<Setmeal> page = new Page<>(paramPage.getPage(),paramPage.getPageSize());
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(paramPage.getName()!=null,Setmeal::getName,paramPage.getName());
        IPage<Setmeal> iPage = setmealDao.selectPage(page, lqw);
        IPage<SetmealDto> page1 = new Page<>();
        page1.setTotal(iPage.getTotal());
        List<SetmealDto> list = page.getRecords().stream().map(aa -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(aa, setmealDto);
            setmealDto.setCategoryName(categoryService.findByCid(aa.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        page1.setRecords(list);
        return page1;
    }
    @CacheEvict(value = "Setmeal",allEntries = true)
    @Override
    public int add(SetmealDto setmealDto) {
        int i = setmealDao.insert(setmealDto);
        int a = 0;
        if (setmealDto.getSetmealDishes() != null && setmealDto.getSetmealDishes().size() > 0) {
            for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
                setmealDish.setSetmealId(setmealDto.getId());
                a+=setmealDishService.add(setmealDish);
            }
        }
        return i>0&a==setmealDto.getSetmealDishes().size()?1:0;
    }

    @Override
    public SetmealDto findById(long id) {
        Setmeal setmeal = setmealDao.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishService.findBySid(id));
        return setmealDto;
    }
    @CacheEvict(value = "Setmeal",allEntries = true)
    @Override
    public int update(SetmealDto setmealDto) {
        int i = setmealDao.updateById(setmealDto);
        setmealDishService.delete(setmealDto.getId());
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        int a=0;
        if (setmealDishList!=null&&setmealDishList.size()>0){
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmealDto.getId());
                a+=setmealDishService.add(setmealDish);
            }
        }
        return i>0&&a==setmealDishList.size()?1:0;
    }
    @CacheEvict(value = "Setmeal",allEntries = true)
    @Override
    public int delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        Long aLong = setmealDao.selectCount(lqw);
        if (aLong>0){
            throw new CustomException("删除的套餐中有启售的套餐，禁止删除！");
        }
        int i = setmealDao.deleteBatchIds(ids);
        for (Long id : ids) {
            setmealDishService.delete(id);
        }
        return i==ids.size()?1:0;
    }
    @CacheEvict(value = "Setmeal",allEntries = true)
    @Override
    public int updateStatus(int status, List<Long> ids) {
        int a=0;
        for (Long id : ids) {
            Setmeal setmeal = setmealDao.selectById(id);
            setmeal.setStatus(status);
            a+=setmealDao.updateById(setmeal);
        }
        return a==ids.size()?1:0;
    }

}
